package com.htx.oldbookpavilion.web.home;

import com.htx.oldbookpavilion.common.StandardPageData;
import com.htx.oldbookpavilion.common.StandardResponse;
import com.htx.oldbookpavilion.entity.BookInfo;
import com.htx.oldbookpavilion.entity.BookTypeDict;
import com.htx.oldbookpavilion.service.homeservice.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 主页相关操作控制器
 */

@RestController
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 分页搜索书籍
     * @param sPageData
     * @return
     */
    @RequestMapping("/searchBooksByPage")
    public StandardResponse searchBooksByPage(@RequestBody StandardPageData<BookInfo> sPageData){
        StandardResponse<StandardPageData<BookInfo>> sResponse = new StandardResponse<>();
        sPageData.setStartIndex(sPageData.calStartIndex());
        sPageData = homeService.getBooksByPage(sPageData);
        if (sPageData != null && sPageData.getRecords() != null) {
            sResponse.setSuccess(true);
            sResponse.setMsg("获取搜索书籍成功");
            sResponse.setData(sPageData);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("获取搜索书籍失败");
        }
        return sResponse;
    }

    /**
     * 获取书籍类型数据
     * @return
     */
    @RequestMapping("/getBookType")
    public StandardResponse getBookTypeData(){
        StandardResponse<List<BookTypeDict>> sResponse = new StandardResponse<>();
        List<BookTypeDict> bookTypeDicts = homeService.getBookTypeData();
        if (bookTypeDicts == null || bookTypeDicts.size() == 0) {
            sResponse.setSuccess(false);
            sResponse.setMsg("获取书籍标签失败。");
            sResponse.setData(null);
        }else{
            sResponse.setSuccess(true);
            sResponse.setMsg("获取书籍标签成功");
            sResponse.setData(bookTypeDicts);
        }
        return sResponse;
    }

    /**
     * 分页获取分类书籍
     * @param sPageData
     * @return
     */
    @RequestMapping("/getClassifiedBooksByPage")
    public StandardResponse getClassifiedBooksByPage(@RequestBody StandardPageData<BookInfo> sPageData,
                                                     BookTypeDict bookType){
        StandardResponse<StandardPageData<BookInfo>> sResponse = new StandardResponse<>();
        sPageData.setStartIndex(sPageData.calStartIndex());
        String type = sPageData.getSearchParam().getType();
        if (bookType.getTypeName() != null){ //根据parentType查找
            sPageData.getSearchParam().setParentType(type);
            sPageData.getSearchParam().setType(null);
        }
        sPageData = homeService.getBooksByPage(sPageData);
        if (sPageData != null && sPageData.getRecords() != null){
            sResponse.setSuccess(true);
            sResponse.setMsg("获取分类书籍成功");
            sResponse.setData(sPageData);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("获取分类书籍失败");
        }
        return sResponse;
    }
}
